<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JALocation feature of the component
*/
class JAJobBoardControllerJALocation extends JAJobBoardController {

    /**
     * Constructor
     */
    function __construct( $location = array() )
    {
    	
        parent::__construct( $location );   
        checkPermission_Administrator('location');   
        // Register Extra tasks
        $this->registerTask( 'add',     'edit' );
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'unpublish', 	'publish');
    }
    
    /**
    * Display current jalocation of the component to administrator
    * 
    */
    function display(){
    	
    	switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', false );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jalocation');
				$model->checkout();
			} break;
			case 'edit':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jalocation');
				$model->checkout();
			} break;
			case 'show_import':
			{
				JRequest::setVar( 'hidemainmenu', 1 );				
				JRequest::setVar( 'layout', 'show_import' );								
			} break;
		}
		
		parent::display();
    }
    
    /**
     * Import locations via file
     * 
     * */
    function import(){
    	global $option;
		$db = JFactory::getDBO();
		$model =& $this->getModel('jalocation');
		
		if(isset($_FILES) && $_FILES['xml_file']['name']){
			$filename = $_FILES['xml_file']['name'];
			$type = substr($filename, -3, 3);
			
			if($type != 'xml'){
				JError::raiseWarning(1, JText::_("File format is incorrect").". ".JText::_("The file must be XML type"));
				return $this->setRedirect("index.php?option=$option&view=jalocation&task=show_import");
			}
		    
			jimport('joomla.filesystem.file');
			JFile::upload($_FILES['xml_file']['tmp_name'], JPATH_ROOT.DS."cache".DS.$filename);
			
			// Load parser
			require_once( JPATH_SITE.DS."components".DS."com_jajobboard".DS."helper".DS."xml_parser.php" );
			//Set up the parser object
			$parser = new XMLParser( file_get_contents(JPATH_ROOT.DS."cache".DS.$filename) );			
			//Work the magic...
			$parser->Parse();		
			
			$data = @$parser->document->tagChildren[3]->tagChildren[0];
					
			if( ! $data ){
				JError::raiseWarning(1, JText::_("File format is incorrect").". ".JText::_("The file must be XML Workbook"));
				return $this->setRedirect("index.php?option=$option&view=jalocation&task=show_import");
			}
			
			$parentID = array();
			$parentID[1] = 0;
			$index = 1;			
			$idChange=0;
			foreach ($data->tagChildren as $row){
				if( $row->tagName == 'row' ){
					$change = false;
					if( isset( $row->tagChildren[0] ) ){						
						if( isset($row->tagChildren[0]->tagAttrs['ss:index']) ){							
							if ($row->tagChildren[0]->tagAttrs['ss:index'] != "" && $row->tagChildren[0]->tagAttrs['ss:index'] > $index ){
								$change = true;
								$index = $row->tagChildren[0]->tagAttrs['ss:index'];
							}
						}else $index = 1;
						
						if ( $index == 1 ) $parentID[1] = 0;
						
						if( isset( $row->tagChildren[0]->tagChildren[0]) ){
							$cat = $row->tagChildren[0]->tagChildren[0];
							if ( $cat->tagName == 'data' ){
								if( $change && $index > 1){
									$parentID[$index] = $idChange;									
								}
								
								$i=1;
								if( !empty($row->tagChildren[0]->tagAttrs['ss:index']) ){
									$i = $row->tagChildren[0]->tagAttrs['ss:index'];									
								}
								$idChange = $model->storeData($cat->tagData, $parentID[$i]);
							}
						}
					}
				}
			}
			
    		$msg = JText::_('Successfully imported locations');
		}else{
			$msg = JText::_('There was an error when upload to the Server');
		}
		$this->setRedirect("index.php?option=$option&view=jalocation", $msg);
    }
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jalocation");
    }   
    /**
    * Remove a jalocation row
    * 
    */   
    function remove(){
    	global $option;
 
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $model =& $this->getModel('jalocation');
        if(($n = $model->remove()) < 0){
             JError::raiseWarning( 500, $row->getError() );
		}
		
		$this->setMessage(JText::_('Successfully deleted location(s)'));
		$this->setRedirect("index.php?option=$option&view=jalocation");
	}
	
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$cache = & JFactory::getCache($option);
		$cache->clean();

		$model	=& $this->getModel('jalocation');
		$post	= JRequest::get('post');
		
		// allow name only to contain html
		$post['name'] = JRequest::getVar( 'name', '', 'post', 'string', JREQUEST_ALLOWHTML );
		
		$paramsField = JRequest::getVar( 'params', null, 'post', 'array' );				
		if ($paramsField)
		{	
			$params = new JParameter('');
			$data = array();
			foreach ($paramsField as $k => $v) {
				$params->set($k, $v);					
			}					
			$post['params'] = $params->toString();
		}
			
		$model->setState( 'request', $post );
		
		if ($id = $model->store()) {
			
			if ((isset($cid[0]))&&$cid[0]!=0)
				$msg = JText::_( 'Successfully updated location' );
			else 
				$msg = JText::_( 'Successfully created location' );
		} else {
			$msg = JText::_( 'Error found!Fail to save location' );
		}
		
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jalocation&task=edit&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jalocation", $msg );
				break;
		}
	}	
	
	/**
	* change Is_Published status
	*/
	/**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function publish(){
		global  $option;
		if($this->getTask()=='unpublish'){
			$publish = 0;
		}
		else{
			$publish = 1;
		}
		
		$model = $this->getModel('jalocation');
				
		if (!$model->dopublish($publish)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			exit();
		}
		$cache = & JFactory::getCache($option);
		$cache->clean();	
		$this->setRedirect("index.php?option=$option&view=jalocation", "Successfully updated location(s)!");
	}		
}
?>

